package cn.gpnusz.oliverwiki.service;

import cn.gpnusz.oliverwiki.entity.Ebook;
import cn.gpnusz.oliverwiki.entity.EbookExample;
import cn.gpnusz.oliverwiki.mapper.EbookMapper;
import cn.gpnusz.oliverwiki.mapper.MyDocMapper;
import cn.gpnusz.oliverwiki.req.EbookQueryReq;
import cn.gpnusz.oliverwiki.req.EbookSaveReq;
import cn.gpnusz.oliverwiki.resp.EbookQueryResp;
import cn.gpnusz.oliverwiki.resp.PageResp;
import cn.gpnusz.oliverwiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author h0ss
 * @description 电子书系统业务层
 * @date 2021/8/24 - 20:57
 */
@Service
public class EbookService {
    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private MyDocMapper myDocMapper;

    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    /**
     * 按传入条件件查询电子书的业务方法
     *
     * @param ebookQueryReq : 查询条件参数
     * @return : cn.gpnusz.oliverwiki.resp.PageResp<cn.gpnusz.oliverwiki.resp.EbookQueryResp>
     * @author h0ss
     */
    public PageResp<EbookQueryResp> getAllByCondition(EbookQueryReq ebookQueryReq) {
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        if (!ObjectUtils.isEmpty(ebookQueryReq.getName())) {
            criteria.andNameLike("%" + ebookQueryReq.getName() + "%");
        }
        if (!ObjectUtils.isEmpty(ebookQueryReq.getCategory2Id())) {
            criteria.andCategory2IdEqualTo(ebookQueryReq.getCategory2Id());
        }
        if (ebookQueryReq.getPage() != null && ebookQueryReq.getSize() != null) {
            PageHelper.startPage(ebookQueryReq.getPage(), ebookQueryReq.getSize());
        } else {
            PageHelper.startPage(1, 10);
        }
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);
        return getEbookResp(ebookList);
    }

    /**
     * 查询全部电子书数据的业务方法
     *
     * @param ebookQueryReq : 查询（分页）参数
     * @return : cn.gpnusz.oliverwiki.resp.PageResp<cn.gpnusz.oliverwiki.resp.EbookQueryResp>
     * @author h0ss
     */
    public PageResp<EbookQueryResp> getAll(EbookQueryReq ebookQueryReq) {
        if (ebookQueryReq.getPage() != null && ebookQueryReq.getSize() != null) {
            PageHelper.startPage(ebookQueryReq.getPage(), ebookQueryReq.getSize());
        } else {
            PageHelper.startPage(1, 100);
        }
        List<Ebook> ebookList = ebookMapper.selectByExample(null);
        return getEbookResp(ebookList);
    }

    /**
     * 新增或编辑电子书的业务方法
     *
     * @param ebookSaveReq : 保存的电子书数据
     * @author h0ss
     */
    public void save(EbookSaveReq ebookSaveReq) {
        Ebook ebook = new Ebook();
        BeanUtils.copyProperties(ebookSaveReq, ebook);
        // 判断是新增还是编辑
        if (ebook.getId() != null) {
            EbookExample ebookExample = new EbookExample();
            EbookExample.Criteria criteria = ebookExample.createCriteria();
            criteria.andIdEqualTo(ebook.getId());
            ebookMapper.updateByExample(ebook, ebookExample);
        } else {
            ebook.setId(snowFlake.nextId());
            ebook.setViewCount(0);
            ebook.setVoteCount(0);
            ebook.setDocCount(0);
            ebookMapper.insert(ebook);
        }
    }

    /**
     * 删除电子书的业务方法
     *
     * @param id : 要删除的电子书id
     * @author h0ss
     */
    public void delete(Long id) {
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        criteria.andIdEqualTo(id);
        ebookMapper.deleteByExample(ebookExample);
    }

    /**
     * 更新电子书的文章数/点赞数/阅读数等信息的业务方法
     * @author h0ss
     * @date 2021/10/12 22:46
     */
    public void updateEbookInfo() {
        myDocMapper.updateEbookInfo();
    }

    /**
     * 将电子书查询结果封装成PageResp对象的业务方法
     *
     * @param ebookList : 电子书查询结果
     * @return : cn.gpnusz.oliverwiki.resp.PageResp<cn.gpnusz.oliverwiki.resp.EbookQueryResp>
     * @author h0ss
     */
    private PageResp<EbookQueryResp> getEbookResp(List<Ebook> ebookList) {
        List<EbookQueryResp> respList = new ArrayList<>();
        for (Ebook ebook : ebookList) {
            EbookQueryResp ebookQueryResp = new EbookQueryResp();
            BeanUtils.copyProperties(ebook, ebookQueryResp);
            respList.add(ebookQueryResp);
        }
        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总行数:{}", pageInfo.getTotal());
        LOG.info("总页数:{}", pageInfo.getPages());

        PageResp<EbookQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }
}
